# -*- coding: utf-8 -*-
import os

from PyQt4.QtCore import QObject, SIGNAL, SLOT, QString, Qt
from PyQt4.QtGui import *

from config.preferences import SYSTEM, SRC_PATH

try:
    _fromUtf8 = QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class ActionsUiMenuEditarDb(object):

    def __init__(self, uiMenuEditarDb, db):
        """
        
        """
        self.uiMenuEditarDb = uiMenuEditarDb
        self.db = db
        self.connectSlots()
        self.preencherTabela()



    def connectSlots(self):
        """
        
        """
        QObject.connect(
                        self.uiMenuEditarDb.pushButtonSalvar,
                        SIGNAL("clicked()"),
                        self.pushButtonSalvarClicked)
        QObject.connect(
                        self.uiMenuEditarDb.tableWidget,
                        SIGNAL("cellDoubleClicked(int,int)"),
                        self.celulaClicked)


    def preencherTabela(self):
        """
        
        """
        response = self.db.preencherTabelaMenuEditarDb()
        numLinhas = len(response)
        self.uiMenuEditarDb.tableWidget.setSortingEnabled(False)
        self.uiMenuEditarDb.tableWidget.setRowCount(numLinhas)
        headers = ["  Id  ",
                    "Produto",
                    "Radical",
                    "Serial",
                    "Faixa"]
        self.uiMenuEditarDb.tableWidget.setColumnCount(len(headers))
        self.uiMenuEditarDb.tableWidget.setHorizontalHeaderLabels(headers)
        i = 0
        
        for vet in response:
            id_produto = str(vet[0])
            produto = str(vet[1])
            radical = str(vet[2])
            serial = str(vet[3])
            faixa = str(vet[4])
            self.uiMenuEditarDb.tableWidget.setItem(
                                                    i,
                                                    0,
                                                    QTableWidgetItem(
                                                    id_produto))
            self.uiMenuEditarDb.tableWidget.setItem(
                                                    i,
                                                    1,
                                                    QTableWidgetItem(
                                                    produto))
            self.uiMenuEditarDb.tableWidget.setItem(
                                                    i,
                                                    2,
                                                    QTableWidgetItem(
                                                    radical))
            self.uiMenuEditarDb.tableWidget.setItem(
                                                    i,
                                                    3,
                                                    QTableWidgetItem(
                                                    serial))
            self.uiMenuEditarDb.tableWidget.setItem(
                                                    i,
                                                    4,
                                                    QTableWidgetItem(
                                                    faixa))
            i += 1
        
        self.uiMenuEditarDb.tableWidget.setSortingEnabled(True)
        self.uiMenuEditarDb.tableWidget.sortItems(0, 0)
        self.uiMenuEditarDb.tableWidget.resizeColumnsToContents()
        self.uiMenuEditarDb.tableWidget.setFocus()




    def pushButtonSalvarClicked(self):
        """
        
        """
        self.atualizarDadosDb()
        self.uiMenuEditarDb.form.close()



    def celulaClicked(self):
        """
        
        """
        i = 0
        self.celula = int(self.uiMenuEditarDb.tableWidget.currentRow())
        while i < 5:
            self.uiMenuEditarDb.tableWidget.item(
                                                  self.celula,
                                                  i).setData(
                                                  Qt.BackgroundRole,
                                                  QColor(255,0,0))
            i += 1



    def atualizarDadosDb(self):
        """
        
        """
        i = 0
        id_produto = int(self.uiMenuEditarDb.tableWidget.item(
                                                              self.celula,
                                                              0).text())
        produto = str(self.uiMenuEditarDb.tableWidget.item(
                                                           self.celula,
                                                           1).text())
        radical = str(self.uiMenuEditarDb.tableWidget.item(
                                                           self.celula,
                                                           2).text())
        serial = str(self.uiMenuEditarDb.tableWidget.item(
                                                          self.celula,
                                                          3).text())
        faixa = str(self.uiMenuEditarDb.tableWidget.item(
                                                         self.celula,
                                                         4).text())
        self.db.editarDb(id_produto, produto, radical, serial, faixa)
